package com.ns.scs.msg.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.current.config.SymbolsMark;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.msg.bo.MsgBO;
import com.ns.scs.msg.bo.MsgPageBO;
import com.ns.scs.msg.dto.MsgPageDto;
import com.ns.scs.msg.entry.MsgEntry;
import com.ns.scs.msg.entry.MsgUser;
import com.ns.scs.msg.service.IMsgService;
import com.ns.scs.msg.service.IMsgUserService;
import com.ns.scs.msg.vo.MsgVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息操作
 *
 * @author lw
 */
@RestController
@RequestMapping("/msg/user")
@RequiredArgsConstructor
@CrossOrigin
public class MsgController {

    private final IMsgService service;

    private final IMsgUserService msgUserService;


    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "消息模块", name = "增")
    public ResultOk add(@RequestBody MsgBO bo) {
        MsgEntry msgEntry = new MsgEntry();
        BeanUtils.copyProperties(bo, msgEntry);
        msgEntry.setSort((service.count() + 1));
        boolean msgFlag = service.save(msgEntry);
        if (msgFlag) {
            // 消息用户关联
            saveMsgUser(bo, msgEntry);
        }
        return ResultOk.getByBoolean(msgFlag);
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "消息模块", name = "改")
    public ResultOk update(@RequestBody MsgBO bo) {
        MsgEntry msgEntry = new MsgEntry();
        BeanUtils.copyProperties(bo, msgEntry);
        boolean msgFlag = service.updateById(msgEntry);
        if (msgFlag) {
            msgUserService.removeByMsgId(bo.getId());
            // 消息用户关联
            saveMsgUser(bo, msgEntry);
        }
        return ResultOk.getByBoolean(service.updateById(msgEntry));
    }

    /**
     * 消息用户关联
     *
     * @param bo       参数
     * @param msgEntry 消息
     */
    private void saveMsgUser(@RequestBody MsgBO bo, MsgEntry msgEntry) {
        if (!CollectionUtils.isEmpty(bo.getUsers())) {
            List<MsgUser> msgUsers = new ArrayList<>();
            for (Long user : bo.getUsers()) {
                MsgUser msgUser = new MsgUser();
                msgUser.setMsgId(msgEntry.getId());
                msgUser.setUserId(user);
                msgUser.setStatus(SymbolsMark.ZERO);
                msgUsers.add(msgUser);
            }
            msgUserService.saveBatch(msgUsers);
        }
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "消息模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "消息模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "消息模块", name = "详情")
    public ResultOk get(Long id) {
        MsgEntry msgEntry = service.getById(id);
        MsgVO vo = new MsgVO();
        BeanUtils.copyProperties(msgEntry, vo);
        List<Long> users = msgUserService.getByMsgId(id);
        vo.setUsers(users);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "消息模块", name = "查-集合")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody MsgPageBO bo) {
        MsgPageDto dto = new MsgPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<MsgVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }
}
